//
//  MOFinishDoDetail_IdeaViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/6/23.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOFinishDoDetail_IdeaViewController.h"
#import "MOWaitDoDetail_IdeaViewCell.h"

@interface MOFinishDoDetail_IdeaViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation MOFinishDoDetail_IdeaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    
    if ([self.waitModel.wdlx isEqualToString:@"收文处理单"]) {
        [self initData];
    }
    
}

-(void)initData
{
    self.dataArray = [NSMutableArray array];
    NSString *url = [NSString stringWithFormat:@"%@/%@?authorization=%@",ODPSSUYB_ODPSYBQUERYSWCLDCLYJ_URL,self.waitModel.key,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [PPNetworkHelper GET:url parameters:@{} success:^(id responseObject) {
        NSLog(@"已办收文处理单处理意见：%@",responseObject);

        if ([self.typeStr integerValue]==2) {//已办
            
            NSArray *arr = responseObject[@"clyj"];
            for (int i = 0; i<arr.count; i++) {
                
                NSDictionary *dataDic = arr[i];
                
                NSString *key = [NSString passNullForString:dataDic.allKeys[0]];
                if (key.length>0) {
                    NSDictionary *dic = dataDic[key];
                    
                    MOWaitDoDetail_IdeaModel *model = [[MOWaitDoDetail_IdeaModel alloc]init];
                    model.resultName = dic[@"name"];
                    model.duty = dic[@"duty"];
                    model.depyName = dic[@"depyName"];
                    model.suggestion = dic[@"suggestion"];
                    model.dateTime = dic[@"dateTime"];
                    model.processMode = dic[@"processMode"];
                    model.declare = dic[@"declare"];
                    model.openStatus = @"0";
                    
                    [self.dataArray addObject:model];
                }
            }
            [self.tableView reloadData];
            
        }
    } failure:^(NSError *error) {
        NSLog(@"已办收文处理单处理意见：%@",[error localizedDescription]);
    }];
    
    
}


-(void)setFinishIdeaArray:(NSMutableArray *)finishIdeaArray
{
    self.dataArray = [NSMutableArray array];
    for (int i = 0; i<finishIdeaArray.count; i++) {
        
        NSDictionary *dataDic = finishIdeaArray[i];
        
        NSString *key = [NSString passNullForString:dataDic.allKeys[0]];
        if (key.length>0) {
            NSDictionary *dic = dataDic[key];
            
            MOWaitDoDetail_IdeaModel *model = [[MOWaitDoDetail_IdeaModel alloc]init];
            model.resultName = dic[@"name"];
            model.duty = dic[@"duty"];
            model.depyName = dic[@"depyName"];
            model.suggestion = dic[@"suggestion"];
            model.dateTime = dic[@"dateTime"];
            model.processMode = dic[@"processMode"];
            model.declare = dic[@"declare"];
            model.openStatus = @"0";
            
            [self.dataArray addObject:model];
        }
    }
    [self.tableView reloadData];
}



-(void)setListArray:(NSMutableArray *)listArray
{
    self.dataArray = [NSMutableArray array];
    for (int i = 0; i<listArray.count; i++) {
        
        NSDictionary *dataDic = listArray[i];
        
        NSString *key = [NSString passNullForString:dataDic.allKeys[0]];
        if (key.length>0) {
            NSDictionary *dic = dataDic[key];
            
            MOWaitDoDetail_IdeaModel *model = [[MOWaitDoDetail_IdeaModel alloc]init];
            model.resultName = dic[@"name"];
            model.duty = dic[@"duty"];
            model.depyName = dic[@"depyName"];
            model.suggestion = dic[@"suggestion"];
            model.dateTime = dic[@"dateTime"];
            model.processMode = dic[@"processMode"];
            model.declare = dic[@"declare"];
            model.openStatus = @"0";
            
            [self.dataArray addObject:model];
        }
    }
    [self.tableView reloadData];
}

-(void)clickOpenButton:(UIButton *)sender
{
    MOWaitDoDetail_IdeaModel *model = self.dataArray[sender.tag];
    if ([model.openStatus integerValue]==1) {
        model.openStatus = @"0";
    }else{
        model.openStatus = @"1";
    }
    [self.tableView reloadData];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOWaitDoDetail_IdeaModel *model = self.dataArray[indexPath.row];
    
    MOWaitDoDetail_IdeaViewCell *cell = [MOWaitDoDetail_IdeaViewCell cellWithTableView:tableView andIndexPath:indexPath];
    cell.model = model;
    
    cell.rightButton.tag = indexPath.row;
    [cell.rightButton addTarget:self action:@selector(clickOpenButton:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOWaitDoDetail_IdeaModel *model = self.dataArray[indexPath.row];
    return [MOWaitDoDetail_IdeaViewCell heightForMOWaitDoDetail_IdeaViewCellAndModel:model];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}


#pragma mark - 设置顶头分割线
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        if (@available(iOS 8.0, *)) {
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        if (@available(iOS 8.0, *)) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}


#pragma mark -- 懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - StatusBarAndNavigationBarHeight - 41) style:(UITableViewStyleGrouped)];
        _tableView.backgroundColor = KGrayColor_ededed;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}


@end
